<?php

namespace Api\Controller;

/**
 * Class CommentController
 * @package Api\Controller
 */
class CommentController extends PublicController
{
    private $userId;

    public function _initialize()
    {
        parent::_initialize();
        $userId = intval($this->get("id"));
        if (!$userId) {
            $this->json_error("请登录");
        }

        $this->userId = $userId;
    }

    /**
     * 发表评论
     */
    public function setComment()
    {
        $obType = intval($_POST['type']);
        $obId = intval($_POST['id']);
        $comment = $_POST['comment'];

        if (!$obId || !$obType || empty($comment)) {
            $this->json_error("参数有误");
        }

        $result = M('comment')->add([
            'user_id'     => $this->userId,
            'object_type' => $obType,
            'object_id'   => $obId,
            'comment'     => $comment,
            'create_at'   => date('Y-m-d H:i:s'),
        ]);


        if ($result) {
            $this->json_result([], '操作成功');
        } else {
            $this->json_error("操作失败");
        }
    }

    /**
     * 获取指定对象评论列表
     */
    public function getList()
    {
        $objectId = intval($_GET['id']);
        $objectType = intval($_GET['type']);
        if (!$objectId || !$objectType) {
            $this->json_error("参数有误");
        }

        $perPage = !empty($_GET['per_page']) ? intval($_GET['per_page']) : 10;
        $curPage = !empty($_GET['page']) ? intval($_GET['page']) : 1;
        $where = "status = 2 and object_type = $objectType and object_id = $objectId";
        $count = M('comment')->where($where)->count();
        $totalPage = ceil($count / $perPage);
        $list = M('comment')->where($where)->order('zan desc, id desc')->limit(($curPage - 1) * $perPage, $perPage)->select();
        foreach ($list as $k => $v) {
            $userInfo = M('user')->where('id = ' . $v['user_id'])->field('name, photo')->find();
            $list[$k]['user_name'] = $userInfo['name'];
            $list[$k]['user_photo'] = __DATAURL__ . $userInfo['photo'];

            $zan = M('zan')->where(['user_id' => $this->userId, 'object_type' => 2, 'object_id' => $v['id']])->find();
            if ($zan) {
                $list[$k]['my_zan'] = 1;
            } else {
                $list[$k]['my_zan'] = 0;
            }
        }

        $this->json_result([
            'per_page'   => $perPage,
            'total'      => $count,
            'total_page' => $totalPage,
            'cur_page'   => $curPage,
            'list'       => $list,
        ]);
    }

    /**
     * 点赞
     */
    public function setZan()
    {
        $obType = intval($_POST['type']);
        $obId = intval($_POST['id']);

        if (!$obId || !$obType) {
            $this->json_error("参数有误");
        }

        if (M('zan')->where([
            'user_id'     => $this->userId,
            'object_type' => $obType,
            'object_id'   => $obId,
        ])->find()
        ) {
            $this->json_error("已点赞");
        }

        $result = M('zan')->add([
            'user_id'     => $this->userId,
            'object_type' => $obType,
            'object_id'   => $obId,
            'create_at'   => date('Y-m-d H:i:s'),
        ]);

        if ($obType == 1) {
            M('news')->where(['id' => $obId])->setInc('zan');
        } elseif ($obType == 2) {
            M('comment')->where(['id' => $obId])->setInc('zan');
        }

        if ($result) {
            // 点赞 + 10 积分
            M('user')->where(['id' => $this->userId])->setInc('point', 10);
            M('point_log')->add(['user_id' => $this->userId, 'point' => 10, 'type' => 3, 'remark' => '点赞送积分', 'create_at' => date('Y-m-d H:i:s')]);

            $this->json_result([], '操作成功');
        } else {
            $this->json_error("操作失败");
        }
    }
}